package com.ht.activiti.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import com.ht.activiti.constant.SystemConstant;
import com.ht.activiti.service.ExeclService;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.model.StylesTable;
import org.apache.poi.xssf.usermodel.*;
import org.apache.xmlbeans.SchemaType;
import org.apache.xmlbeans.impl.schema.SchemaTypeImpl;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFont;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.impl.CTFontImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 此处为我自己用的将execl转换为想要的格式
 * @author zhangfc
 * @date 2021/10/29 8:14
 */
@RestController
@RequestMapping("/Execl")
public class ExeclController {

    @Autowired
    ExeclService execlService;

    @PostMapping("/execlTransFormFile")
    public  String  execlTransFormFile(@RequestParam("file") MultipartFile multipartFile,HttpServletResponse httpServletResponse){
        try {
            if (multipartFile == null){
                return "文件不能为空";
            }
            InputStream inputStream = multipartFile.getInputStream();
            List<Map<String,Object>> list = execlService.execlTransFormFile(inputStream, multipartFile.getOriginalFilename());
            inputStream.close();

            //重新生成Execl文件
            XSSFWorkbook sheets = new XSSFWorkbook();
            XSSFSheet sheet = sheets.createSheet("转换表格");
            //为单元格设置一个默认宽度
            sheet.setDefaultColumnWidth(20);
            sheet.setVerticallyCenter(true);
            //第一行
            XSSFRow firstRow = sheet.createRow(0);


            //格式1 红色居中
            XSSFFont font1 = sheets.createFont();
            font1.setColor(Font.COLOR_RED);
            XSSFCellStyle cellStyle1 = sheets.createCellStyle();
            cellStyle1.setAlignment(CellStyle.ALIGN_CENTER);
            cellStyle1.setFont(font1);


            //正文格式2 蓝色居中
            XSSFFont font2 = sheets.createFont();
            font2.setColor(HSSFColor.BLUE.index2);
            XSSFCellStyle cellStyle2 = sheets.createCellStyle();
            cellStyle2.setAlignment(CellStyle.ALIGN_CENTER);
            cellStyle2.setFont(font2);
            //设置标题
            Map<String, Object> first = CollectionUtil.getFirst(list);
            int line = 0;
            for (String key : first.keySet()) {
                XSSFCell cell = firstRow.createCell(line);
                XSSFRichTextString xssfRichTextString = new XSSFRichTextString(key);
                cell.setCellValue(xssfRichTextString);
                cell.setCellStyle(cellStyle1);
                line++;
            }
            //向表格存入数值
            for (int i = 0; i < list.size(); i++) {
                XSSFRow row = sheet.createRow(i + 1);
                int j = 0;
                for (String key : list.get(i).keySet()) {
                    XSSFCell cell = row.createCell(j);
                    XSSFRichTextString xssfRichTextString = new XSSFRichTextString(Convert.toStr(list.get(i).get(key)));
                    cell.setCellValue(xssfRichTextString);
                    cell.setCellStyle(cellStyle2);
                    j++;
                }
            }
            String fileName = Convert.toStr(System.currentTimeMillis());


            //八进制输出流
            httpServletResponse.setContentType("application/octet-stream");
            //导出Excel
            httpServletResponse.setHeader("Content-Disposition","attachment;filename="+fileName+".xlsx");
            //刷新缓冲
            httpServletResponse.flushBuffer();
            //供页面下载
            sheets.write(httpServletResponse.getOutputStream());

            return "上传成功";
        } catch (Exception e) {
            e.printStackTrace();
            return "上传失败,原因:"+e.getMessage();
        }
    }


    @GetMapping("/execlTest")
    public void execlTest(HttpServletResponse httpServletResponse) throws IOException {
        Map<String,Object> map  = new HashMap<>();

        //声明一个工作薄 此处创建的文件格式为xlsx所以对象应当为XSSFWorkbook
        XSSFWorkbook workbook  = new XSSFWorkbook();
        //为第一个sheet设置名字
        XSSFSheet sheet = workbook.createSheet("统计表");
        //为单元格设置一个宽度
        sheet.setDefaultColumnWidth(20);


        //声明一个表头
        XSSFRow headRow = sheet.createRow(0);
        XSSFCell templeCell = headRow.createCell(0);
        XSSFRichTextString tempText  = new XSSFRichTextString("表头");
        templeCell.setCellValue(tempText);

        for (int i = 0; i < SystemConstant.ExeclConstant.ROW_LIST.size(); i++) {
            String title  = SystemConstant.ExeclConstant.ROW_LIST.get(i);
            //创建一个单元格
            XSSFCell cell = headRow.createCell(i + 1);
            //创建一个内容对象
            XSSFRichTextString textString = new XSSFRichTextString(title);
            //将内容对象的文字内容写入到单元格中
            cell.setCellValue(textString);
        }

        for (int i = 0; i < 10; i++) {
            XSSFRow row = sheet.createRow(i + 1);
            for (int j = 0; j < 5; j++) {
                XSSFCell cell = row.createCell(j);
                XSSFRichTextString textString = new XSSFRichTextString(Convert.toStr(j));
                cell.setCellValue(textString);
            }
        }

        String fileName = Convert.toStr(System.currentTimeMillis());


        //八进制输出流
        httpServletResponse.setContentType("application/octet-stream");
        //导出Excel
        httpServletResponse.setHeader("Content-Disposition","attachment;filename="+fileName+".xlsx");
        //刷新缓冲
        httpServletResponse.flushBuffer();
        //供页面下载
        workbook.write(httpServletResponse.getOutputStream());
    }

}
